# encoding=utf8
import tornado.web

import WrapCursor
from CommonLib import *
from database.models.DatabaseCommon import *
from database.models.TimingSwitch import TimingSwitch


class AddTimingSwitch(tornado.web.RequestHandler):
    __metaclass__ = ABCMeta

    def post(self):
        r = WrapCursor.WrapCursor()
        try:
            ojson = json.loads(self.request.body)
            t1 = ojson['desc']
            CheckJsonKeys(ojson, 'username', 'userpass')
            CheckAuth(ojson)
        except Exception as e:
            logging.warn('request data not available')
            self.write(r.Error('request data error' + str(e.message)))
            return
        s = Session()
        tming = TimingSwitch(desc=t1)
        s.add(tming)
        # noinspection PyBroadException
        try:
            s.commit()
        except Exception as e:
            self.write(r.Error(u'错误:增加失败'))
            return
        self.write(r.Ok(u'成功'))
        return
